package com.tpshion.cloud.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tpshion.cloud.auth.domain.dto.AddMenuDto;
import com.tpshion.cloud.auth.domain.dto.UpdateMenuDto;
import com.tpshion.cloud.auth.domain.entity.Menu;
import com.tpshion.cloud.auth.mapper.MenuMapper;
import com.tpshion.cloud.auth.service.MenuService;
import com.tpshion.cloud.auth.utils.support.MenuData;
import com.tpshion.cloud.common.support.Result;
import com.tpshion.cloud.core.annotation.Permission;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
public class MenuController {

    private static final Logger log = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private MenuService menuService;

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 获取所有菜单树
     * @return
     */
    @GetMapping("/menus")
    public Result<?> getMenuList(@RequestParam("sysCode") String sysCode){
        log.info("*** getMenuList sysCode:{}",sysCode);
        List<MenuData> allMenuData = menuService.getAllMenuData(sysCode);
        return Result.ok(allMenuData);
    }

    /**
     * 检查菜单标题是否已存在
     */
    @PostMapping("/menu/checkTitle")
    public Result<?> checkTitle(@RequestParam("id") String id, @RequestParam("title") String title){
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title", title);
        queryWrapper.ne("id", id);
        Integer count = menuMapper.selectCount(queryWrapper);
        if(count > 0){
            return Result.fail("标题已存在");
        }
        return Result.ok();
    }

    /**
     * 检查菜单路由是否已存在
     */
    @PostMapping("/menu/checkName")
    public Result<?> checkName(@RequestParam("id") String id, @RequestParam("name") String name){
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        queryWrapper.ne("id", id);
        Integer count = menuMapper.selectCount(queryWrapper);
        if(count > 0){
            return Result.fail("路由已存在");
        }
        return Result.ok();
    }

    /**
     * 更新菜单
     */
    @PostMapping(value = "/menu",params = {"action=upd"})
    @Permission("menu:update")
    public Result<?> updateMenu(@RequestBody @Validated UpdateMenuDto updateMenuDto){
        Menu menu = menuService.updateMenu(updateMenuDto);
        return Result.ok(menu);
    }

    /**
     * 添加菜单
     */
    @PostMapping(value = "/menu",params = {"action=add"})
    @Permission("menu:add")
    public Result<?> addMenu(@RequestBody @Validated AddMenuDto addMenuDto){
        Menu menu = menuService.addMenu(addMenuDto);
        return Result.ok(menu);
    }

    /**
     * 删除菜单
     */
    @PostMapping(value = "/menu", params = {"action=del"})
    @Permission("menu:delete")
    public Result<?> deleteMenu(@RequestBody List<String> menuIds){
        if(CollectionUtils.isEmpty(menuIds)){
            return Result.fail("参数不能为空");
        }
        menuService.deleteMenu(menuIds);
        return Result.ok();
    }

    /**
     * 查询已有菜单服务
     */
    @GetMapping(value = "/menu/services")
    public Result<?> getMenuServices(){
        List<String> menuServices = menuMapper.getMenuServices();
        log.info("*** menuServices:{}",menuServices);
        return Result.ok(menuServices);
    }
}
